package com.blackline.auto.Report;

import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;



import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author Allen
 * @version May 24, 2013
 * @Updated by Joie on Aug 08,2013
 *          <p>
 *          This is to run all 17 standard reports with all 14 roles.
 */


public class RunStdReportsWithAllRoles
{

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger = Logger.getLogger(RunStdReportsWithAllRoles.class);
	UI UI = new UI();
	Parameters parameters;

	int sleepTime = 2000;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
	Date date = new Date();
	String dateString = sdf.format(date);

	TestData testdata = new TestData("TestData/RunStdReportsWithAllRoles.xml");
	
	
	String t2Test2 = testdata.getInput("t2Test2");
	String testStarter = testdata.getInput("testStarter");
	String testInternal2 = testdata.getInput("testInternal2");
	String user = testdata.getInput("user");
	String password = testdata.getInput("password");
	//Get roles
	String[] roles =CommonMethods.getRunParameter("TestData/BL_AllRoles.xml","Role");
    //Get standards reports which need run
	String[] standardReports =CommonMethods.getRunParameter("TestData/BL_StdReports.xml", "StdRpt");


	String desc_string_for_Account_Reconciliation_Summary = "category totals";
	String desc_string_for_Certification_Status_By_Entity = "Summary report";

	@Before
	public void setUp() throws Exception
	{
		FirefoxProfile profile = new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters = new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}

	@Test
	public void RunWithAllRolesT2Test2() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + t2Test2 + " with: " + user);
		UI.LogonPage.Login(driver, t2Test2, user, password);

		// Start run the report with different roles.
		runStdReportsWithSpecificRole(1, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(2, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(3, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(4, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(5, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(6, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(7, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(8, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(9, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(10, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(11, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(12, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(13, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(14, t2Test2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);

	}
	
	
//	@Test
	public void RunWithAllRoles3WXBOSoft() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + t2Test2 + " with: " + user);
		UI.LogonPage.Login(driver, t2Test2, user, password);
		
		// Start run the report with different roles.
		runStdReportsWithSpecificRole(1, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(2, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(3, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(4, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(5, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(6, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(7, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(8, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(9, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(10, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(11, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(12, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(13, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(14, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(15, t2Test2);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		
	}

	
//	@Test
	public void RunWithAllRolesTestStarter() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + testStarter + " with: " + user);
		UI.LogonPage.Login(driver, testStarter, user, password);
		
		// Start run the report with different roles.
		runStdReportsWithSpecificRole(1, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(2, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(3, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(4, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(5, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(6, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(7, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(8, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(9, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(10, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(11, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(12, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(13, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(14, testStarter);
		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(15, testStarter);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
	}
	
//	@Test
	public void RunWithAllRolesTestInternal2() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + testInternal2 + " with: " + user);
		UI.LogonPage.Login(driver, testInternal2, user, password);
		
		// Start run the report with different roles.
//		runStdReportsWithSpecificRole(1, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(2, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(3, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(4, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(5, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(6, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(7, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(8, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(9, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(10, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(11, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(12, testInternal2);
////		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(13, testInternal2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
		runStdReportsWithSpecificRole(14, testInternal2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
//		runStdReportsWithSpecificRole(15, testInternal2);
//		UI.Reporting.VerifyReportResultsUnderReportLog(standardReports);
	
	}
		
	
	
	@After
	public void tearDown() throws Exception
	{
		 CommonMethods.takeScreenShot(this.getClass(), driver);
		 logger.info("===========================================");
		// driver.close();
		// driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	/**
	 * This is to run the 17 standard reports with specific roles
	 * 
	 * @param roleName
	 *            The name of the role to run the reports
	 */
	private void runStdReportsWithSpecificRole(int sequence, String site) throws Exception
	{
		// Switch to different roles
		UI.SessionMenu.switchRoleBaseOnSequence(sequence);

		// Go to Enhanced Reporting page
		Thread.sleep(sleepTime);
		driver.get(site);
		Thread.sleep(sleepTime);

		// ======== Go to Accounts node========
		logger.info("=====Accounts Node=====");
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(sleepTime);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(sleepTime);

		// Run User Role Assignment
		logger.info("Going to run: " + standardReports[0]);
		UI.Reporting.Name_text_field.clear();
		UI.Reporting.Description_text_field.clear();
		UI.Reporting.Name_text_field.clear();
		
		UI.Reporting.Name_text_field.sendKeys(standardReports[0]);
		UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
		Thread.sleep(sleepTime);

		UI.Reporting.Run_linkOfSearchedResult.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Run_Report_button.click();
		Thread.sleep(sleepTime);
		UI.Reporting.OK_button.click();
		Thread.sleep(sleepTime);

		// ======== Run Report under Analysis node.========
		logger.info("=====Analysis Node=====");
		UI.Reporting.Analysis_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		for (int i = 1; i < 7; i++)
		{
			logger.info("Going to run: " + standardReports[i]);

			// Following if to avoid the conflict of
			// "Account Reconciliation Summary"
			// and "Account Reconciliation Summary with Open Items".

			UI.Reporting.Name_text_field.clear();
			UI.Reporting.Description_text_field.clear();
			UI.Reporting.Name_text_field.clear();

			if (standardReports[i].equals("Account Reconciliation Summary"))
			{
				UI.Reporting.Description_text_field.sendKeys(desc_string_for_Account_Reconciliation_Summary);
				Thread.sleep(sleepTime);
			}

			UI.Reporting.Name_text_field.sendKeys(standardReports[i]);
			UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(sleepTime);

			UI.Reporting.Run_linkOfSearchedResult.click();
			Thread.sleep(sleepTime);
			UI.Reporting.Run_Report_button.click();
			Thread.sleep(sleepTime);
			UI.Reporting.OK_button.click();
			Thread.sleep(sleepTime);
		}

		// ======== Run Report under Information node.========
		logger.info("=====Information Node=====");
		UI.Reporting.Information_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		for (int i = 7; i < 11; i++)
		{
			logger.info("Going to run: " + standardReports[i]);
			UI.Reporting.Name_text_field.clear();
			UI.Reporting.Description_text_field.clear();
			UI.Reporting.Name_text_field.clear();
			
			UI.Reporting.Name_text_field.sendKeys(standardReports[i]);
			UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(sleepTime);

			UI.Reporting.Run_linkOfSearchedResult.click();
			Thread.sleep(sleepTime);
			UI.Reporting.Run_Report_button.click();
			Thread.sleep(sleepTime);
			UI.Reporting.OK_button.click();
			Thread.sleep(sleepTime);
		}

		// ======== Run Report under Status node.========
		logger.info("=====Status Node=====");
		UI.Reporting.Status_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		for (int i = 11; i < 17; i++)
		{
			logger.info("Going to run: " + standardReports[i]);
			UI.Reporting.Name_text_field.clear();
			UI.Reporting.Description_text_field.clear();
			UI.Reporting.Name_text_field.clear();

			if (standardReports[i].equals("Certification Status By Entity"))
			{
				UI.Reporting.Description_text_field.sendKeys(desc_string_for_Certification_Status_By_Entity);
				Thread.sleep(sleepTime);
			}

			UI.Reporting.Name_text_field.sendKeys(standardReports[i]);
			UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(sleepTime);

			UI.Reporting.Run_linkOfSearchedResult.click();
			Thread.sleep(sleepTime);
			UI.Reporting.Run_Report_button.click();
			Thread.sleep(sleepTime);
			UI.Reporting.OK_button.click();
			Thread.sleep(sleepTime);
		}

		logger.info("==========Current time is: " + dateString + "; Role " + sequence + " done.==========");

	}


}
